package club.dataresources.controller;

import club.dataresources.bo.OrderBo;
import club.dataresources.common.R;
import club.dataresources.entity.Order;
import club.dataresources.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
 * @author
 * @create 2018-11-12 1:28
 **/
@RequestMapping(value = "/admin")
@Controller
public class AdminOrderController {
    @Autowired
    private OrderService orderService;

    @RequestMapping("/addOrder")
    public @ResponseBody
    R addOrder(@RequestBody Order order){
        System.out.println("=====================新增订单=======================");
        try {
            if(orderService.addOrder(order)){
                return R.ok().put("code",200).put("msg","新增成功");
            }else{
                return R.error().put("code",500).put("msg","新增失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }


    @RequestMapping("/updateOrder")
    public @ResponseBody
    R updateOrder(@RequestBody Order order){
        System.out.println("=====================修改订单=======================");
        try {
            if(orderService.updateOrder(order)){
                return R.ok().put("code",200).put("msg","修改成功");
            }else{
                return R.error().put("code",500).put("msg","修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/removeOrder")
    public @ResponseBody
    R findByOrderId(@RequestParam("orderId")Integer orderId){
        System.out.println("=====================删除订单=======================");
        try {
            return R.ok().put("data",orderService.findByOrderId(orderId)).put("code",200).put("msg","查询完成");
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/listOrder")
    public @ResponseBody
    R listOrder(@RequestBody OrderBo orderBo){
        System.out.println("=====================列表查询订单=======================");
        try {
            return R.ok().put("data",orderService.listOrder(orderBo.getPage(),orderBo.getRows(),orderBo)).put("code",200).put("msg","查询完成");
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }
}
